package com.alipay.sdk.util;

import android.os.Bundle;
import android.text.TextUtils;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Locale;
import java.util.Set;

/**
 * @ProjectName: 团餐
 * @ClassName: TextUtil
 * @Description: java类作用描述
 * @Author: bd-001935
 * @CreateDate: 4/18/22 10:47 AM
 * @UpdateUser: bd-001935
 * @UpdateDate: 4/18/22 10:47 AM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class TextUtil {

    /**
     * 手机号用****号隐藏中间数字
     *
     * @param phone
     * @return
     */
    public static String formatMobile(String phone) {
        if (TextUtils.isEmpty(phone))
            return "";
        String phone_s = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        return phone_s;
    }

    public static String formatPriceNoSymbol(float amout) {
        return formatPrice(amout, false);
    }

    public static String formatPriceNoSymbol(double amout) {
        return formatPrice(amout, false);
    }

    public static String formatPriceNoSymbol(BigDecimal amout) {
        return formatPrice(amout.doubleValue(), false);
    }

    public static String formatPrice(double amout) {
        return formatPrice(amout, true);
    }

    public static String formatPrice(BigDecimal amout) {
        return formatPrice(amout.doubleValue(), true);
    }

    public static String formatPriceNoSymbol(String amount) {
        if (TextUtils.isEmpty(amount))
            return formatPrice(0, false);
        Double price = Double.parseDouble(amount);
        if (price == null) {
            price = 0d;
        }
        return formatPrice(price, false);
    }

    public static String formatPrice(String amount) {
        if (TextUtils.isEmpty(amount))
            return formatPrice(0, true);
        Double price = Double.parseDouble(amount);
        if (price == null) {
            price = 0d;
        }
        return formatPrice(price, true);
    }

    public static String formatPrice(double amount, boolean withSymbol) {
        if (withSymbol)
            return String.format(Locale.getDefault(), "￥%1$.2f", amount);
        return String.format(Locale.getDefault(), "%1$.2f", amount);
    }

    public static String formatIdNum(String id) {
        if (TextUtils.isEmpty(id))
            return "";
        return id.replaceAll("(?<=\\w{6})\\w(?=\\w{4})", "*");
    }

    public static String shortOutTradeNo(String s) {
        String tradeNo = s;
        if (!TextUtils.isEmpty(tradeNo) && tradeNo.length() > 8)
            tradeNo = tradeNo.substring(tradeNo.length() - 8);
        return tradeNo;
    }

    public static boolean isEmpty(String s) {
        if (TextUtils.isEmpty(s))
            return true;
        if (s.equalsIgnoreCase("null"))
            return true;
        return false;
    }

    private HashMap<String, Object> getBundleData(Bundle bundle) {
        HashMap<String, Object> map = new HashMap<>();
        if (bundle == null)
            return map;
        Set<String> keySet = bundle.keySet();
        for (String key : keySet) {
            Object value = bundle.get(key);
            map.put(key, value);
        }
        return map;
    }
}
